package mdm.led.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.entity.five.FiveDetails;
import mdm.led.entity.five.FiveProcedure;
import mdm.led.service.FiveProcedureService;

/**
 *
 * @author :Monarch丿He
 * @date :2018年11月2日
 * @explain :流程管控 WEB
 */
@Controller
@RequestMapping("fiveProcedureWeb")
@Api(description = "流程管控 WEB")
public class FiveProcedureWebController {

	private static final Logger LOGGER = LoggerFactory.getLogger(FiveProcedureWebController.class);

	@Autowired
	private FiveProcedureService fiveProcedureServiceImpl;

	@ApiOperation("督查角色添加流程表格")
	@ResponseBody
	@RequestMapping(value = "addFiveProcedure", method = RequestMethod.POST)
	public AjaxResponse addFiveProcedure(@RequestBody FiveProcedure fiveProcedure) {
		LOGGER.info("\n---------督查角色添加流程表格----------");
		Boolean iRet = fiveProcedureServiceImpl.addFiveProcedure(fiveProcedure);
		LOGGER.info("\n-----响应值:" + iRet + "-------");
		if (iRet) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "添加成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "添加失败");
		}
	}

	@ResponseBody
	@ApiOperation("督查角色删除流程表格")
	@RequestMapping(value = "delFiveEstate", method = RequestMethod.PUT)
	public AjaxResponse delFiveEstate(@RequestParam("procedureId") Integer id) {
		LOGGER.info("\n---------督查角色删除流程表格----------");
		int iRet = fiveProcedureServiceImpl.updateFiveEstate(id);
		LOGGER.info("\n-----响应值:" + iRet + "-------");

		if (iRet > 0) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "删除成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "删除失败");
		}
	}

	@RequestMapping(value = "findFiveProcedure", method = RequestMethod.GET)
	@ApiOperation("查询所有流程表信息(可按名称模糊查询)")
	@ResponseBody
	public AjaxResponse findFiveProcedure(@RequestParam(value = "name", required = false) String name,
			@RequestParam("pageSize") Integer pageSize, @RequestParam("currPage") Integer currPage) {
		LOGGER.info("\n---------查询所有流程表信息(可按名称模糊查询)----------");
		// 开启分页
		Page<FiveProcedure> page = PageHelper.startPage(currPage, pageSize);
		// 需要分页的数据集合
		List<FiveProcedure> list = fiveProcedureServiceImpl.findFiveProcedure(name);
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		// 数据
		data.put("inspectorList", list);
		// 当前页
		data.put("currPage", page.getPageNum());
		// 页总数
		data.put("pageCount", page.getPages());
		// 页大小
		data.put("pageSize", page.getPageSize());
		// 总条数
		data.put("totalCount", page.getTotal());
		LOGGER.info("\n-----响应值:" + data + "-------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
	}

	@ApiOperation("根据流程管表id 查询表格详情信息")
	@ResponseBody
	@RequestMapping(value = "findProcedureById", method = RequestMethod.GET)
	public AjaxResponse findProcedureById(@RequestParam("procedureId") Integer id) {
		LOGGER.info("\n---------查询所有流程表信息(可按名称模糊查询)----------");
		FiveProcedure fiveProcedure = fiveProcedureServiceImpl.findProcedureById(id);
		LOGGER.info("\n--------- 结果 :" + JSON.toJSONString(fiveProcedure) + "----------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, fiveProcedure);
	}

	@ApiOperation("查询流程管控某表下所有详情表 ")
	@ResponseBody
	@RequestMapping(value = "findFiveDetailsList", method = RequestMethod.GET)
	public AjaxResponse findFiveDetailsList(@RequestParam(value = "username", required = false) String username,
			@RequestParam("procedureId") Integer procedureId, @RequestParam("pageSize") Integer pageSize,
			@RequestParam("currPage") Integer currPage) {

		LOGGER.info("\n-------------- 查询流程管控某表下所有详情表 ------------");
		LOGGER.info("\n入参:" + "username:" + username + "\nprocedureId:" + procedureId + "\npageSize:" + pageSize
				+ "\ncurrPage:" + currPage);
		// 开启分页
		Page<FiveDetails> page = PageHelper.startPage(currPage, pageSize);
		List<FiveDetails> list = fiveProcedureServiceImpl.findFiveDetailsList(username, procedureId);
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		// 数据
		data.put("inspectorList", list);
		// 当前页
		data.put("currPage", page.getPageNum());
		// 页总数
		data.put("pageCount", page.getPages());
		// 页大小
		data.put("pageSize", page.getPageSize());
		// 总条数
		data.put("totalCount", page.getTotal());
		LOGGER.info("\n-----响应值:" + JSON.toJSONString(data) + "-------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
	}

	@ApiOperation("根据详情表 ID 查看详情信息 ")
	@ResponseBody
	@RequestMapping(value = "findFiveDetailsById", method = RequestMethod.GET)
	public AjaxResponse findFiveDetailsById(@RequestParam("detailsId")Integer detailsId) {

		LOGGER.info("\n-------------- 根据详情表 ID 查看详情信息 ------------");
		LOGGER.info("\n-------------- 入参:"+detailsId+" ------------");
		FiveDetails fiveDetails = fiveProcedureServiceImpl.findFiveDetailsById(detailsId);
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(fiveDetails) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, fiveDetails);
	}

}
